package com.tensquare.problem.controller;
import java.util.List;
import java.util.Map;

import com.tensquare.problem.client.LabelClient;
import com.tensquare.problem.pojo.Pl;
import com.tensquare.problem.pojo.Reply;
import com.tensquare.problem.service.PLService;
import com.tensquare.problem.service.ReplyService;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.problem.pojo.Problem;
import com.tensquare.problem.service.ProblemService;

import entity.PageResult;
import entity.Result;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/qa")
public class ProblemController {

	@Autowired
	private ProblemService problemService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private LabelClient labelClient;
	@Autowired
	private JwtUtil jwtUtil;

	@Autowired
	private PLService plService;
	@Autowired
	private ReplyService replyService;
	@Autowired
	private RedisTemplate redisTemplate;
	//查询 热门标签  根据 使用数量判断  倒序显示
	@RequestMapping("/label/findLabelAll")
	public Result findLabelAll(){
		Result result = labelClient.findLabelAll();
		return result;
	}
	//查询  标签
	@RequestMapping(value = "/findByProblemid/{problemid}",method = RequestMethod.GET)
	public List<Pl> findByproblemid(@PathVariable String problemid){
		List<Pl> byProblemid = plService.findByProblemid(problemid);

		return byProblemid;
	}

	//查询 回答问题
	@RequestMapping(value = "findReplyByProblemid/{problemid}",method = RequestMethod.GET)
	public List<Reply> findByProblemid(@PathVariable String problemid){
		return replyService.findByPid(problemid);
	}

	/**
	 * 根据 id 关注 标签
	 * @return
	 */
	@RequestMapping(value = "/addFans/{id}",method= RequestMethod.PUT)
	public Result addFans(@PathVariable String id){
		String flag = (String) redisTemplate.opsForValue().get(id);
//如果 缓存 存在  说明 已关注过
		if(flag != null && flag.equals("1")){
			return new Result(false,StatusCode.ERROR,"已加关注","");
		}
		//不存在 则点赞
		System.out.println("关注："+id);
		labelClient.addFans(id);
		return new Result(true,StatusCode.OK,"关注成功","");
	}

	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public List<Problem> findAll(){
		return problemService.findAll();
	}
	
	/**
	 * 根据ID查询
	 * @param ID
	 * @return
	 */
	@RequestMapping(value = "/label/{labelid}")
	public Result findLabelById(@PathVariable String labelid){
		Result result = labelClient.findById(labelid);
		return result;
	}

	/**
	 * 根据标签ID查询最新问题列表
	 * @param labelid
	 * @return
	 */
	@RequestMapping(value = "/newlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
	public Result newList(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
		Page<Problem> pageDatas = problemService.newList(labelid, page, size);
		return new Result(true,StatusCode.OK,"查询成功",new PageResult<Problem>(pageDatas.getTotalElements(),pageDatas.getContent()));
	}

	//热门回答
	@RequestMapping(value = "/hotlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
	public Result hotList(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
		Page<Problem> pageDatas = problemService.hotList(labelid, page, size);
		return new Result(true,StatusCode.OK,"查询成功",new PageResult<Problem>(pageDatas.getTotalElements(),pageDatas.getContent()));
	}

	//等待回答列表
	@RequestMapping(value = "/waitlist/{labelid}/{page}/{size}",method = RequestMethod.GET)
	public Result waitList(@PathVariable String labelid,@PathVariable int page,@PathVariable int size){
		Page<Problem> pageDatas = problemService.waitList(labelid, page, size);
		return new Result(true,StatusCode.OK,"查询成功",new PageResult<Problem>(pageDatas.getTotalElements(),pageDatas.getContent()));
	}
	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public PageResult<Problem> findPage(@PathVariable int page,@PathVariable int size){		
		Page<Problem> pageList = problemService.findPage(page, size);		
		return new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent());		
	}
	
	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.POST)
	public PageResult<Problem> findSearch(@RequestBody Map searchMap ,@PathVariable int page,@PathVariable int size){		
		Page<Problem> pageList = problemService.findSearch(searchMap, page, size);
		return new PageResult<Problem>(pageList.getTotalElements(), pageList.getContent());		
	}
	
	/**
	 * 增加
	 * @param problem
	 */
	@RequestMapping(value="/",method=RequestMethod.POST)
	public Result add(@RequestBody Problem problem  ){
		Claims claims=(Claims)request.getAttribute("user_claims");
		if(claims==null){
			return new Result(false,StatusCode.ACCESSERROR,"无权访问");
		}
		problem.setUserid(claims.getId());
		problemService.add(problem);	
		
		return new Result(true,0,"增加成功");
	}
	
	/**
	 * 修改
	 * @param problem
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public Result update(@RequestBody Problem problem,@PathVariable String id ){
		problem.setId(id);
		problemService.update(problem);		
		return new Result(true,0,"");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		problemService.delete(id);
		return new Result(true,0,"");
	}
	
}
